package com.yanqu.road.logic.helper;

import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossChannelHelper {

    // 【总开关】
    private static boolean open = false;

    // 游戏服    指定    跨地区专用跨服
    private static Map<Long, Long> gameAppointMap = new ConcurrentHashMap<>();

    // 跨地区专用跨服    指定    跨地区服
    private static Map<Long, Long> cross3AppointMap = new ConcurrentHashMap<>();

    public synchronized static void refresh() {
        try {

            String s = ChannelConfig.CROSS_CHANNEL_TEST_PARAM;
            List<String> paramList = StringUtils.stringToStringList(s, "\\|");

            // 判断开关状态
            if (!paramList.get(0).equals("1")) {

                setDefaultValue();

            } else {

                Map<Long, Long> tmpGameMap = new ConcurrentHashMap<>();
                Map<Long, Long> tmpCross3Map = new ConcurrentHashMap<>();
                for (int i = 1; i < paramList.size(); i++) {
                    String param = paramList.get(i);
                    List<Long> longList = StringUtils.stringToLongList(param, "-");

                    long type = longList.get(0);
                    long serverId = longList.get(1);
                    long appointId = longList.get(2);

                    if (type == 1) {
                        tmpGameMap.put(serverId, appointId);
                    } else if (type == 2) {
                        tmpCross3Map.put(serverId, appointId);
                    } else {
                        throw new RuntimeException("ChannelConfig.CROSS_CHANNEL_TEST_PARAM error! value = " + s);
                    }
                }
                open = true;
                gameAppointMap = tmpGameMap;
                cross3AppointMap = tmpCross3Map;

            }
        } catch (Exception e) {
            e.printStackTrace();
            // 设置默认值
            setDefaultValue();
        }
    }

    private static void setDefaultValue() {
        open = false;
        gameAppointMap = new ConcurrentHashMap<>();
        cross3AppointMap = new ConcurrentHashMap<>();
    }

    /**
     * 获取专用跨服ID
     */
    public static long getCross3Id(long gameServerId) {
        if (open && gameAppointMap.containsKey(gameServerId)) {
            return gameAppointMap.get(gameServerId);
        }
        return ChannelConfig.CHANNEL_ACTIVITY_CROSS_SERVER_ID;
    }

    /**
     * 获取指定跨地区服ID
     *
     * @return 0表示没有指定
     */
    public static long getAppointChannelServerId(long cross3Id) {
        if (open && cross3AppointMap.containsKey(cross3Id)) {
            return cross3AppointMap.get(cross3Id);
        }
        return 0;
    }

    /**
     * 是否专用跨服
     */
    public static boolean isCross3(long crossServerId) {
        if (ChannelConfig.CHANNEL_ACTIVITY_CROSS_SERVER_ID == crossServerId) {
            return true;
        }
        return open && (gameAppointMap.containsValue(crossServerId) || cross3AppointMap.containsKey(crossServerId));
    }

    /**
     * 获取临时最大榜单长度
     */
    public static int getTempRankSize() {
        return 500;
    }

    /**
     * 获取区域贡献最大长度
     */
    public static int getTempChannelRankPartNum() {
        return 100;
    }

    /**
     * 同步榜单单次最大长度
     */
    public static int getSyncRankListSize() {
        return 100;
    }
}
